package leetcode.list;

public class Lc92 {


    public ListNode reverseBetween(ListNode head, int left, int right) {
        if (left == right) return head;
        ListNode leftNode = null;
        ListNode rightNode = null;
        ListNode dummyHead = head;
        ListNode leftPre = null;
        int idx = 1;
        while (head != null && idx != left) {
            leftPre = head;
            head = head.next;
            idx++;
        }

        leftNode = head;

        ListNode pre = null;
        while (head != null && idx != right) {
            ListNode cur = head;
            head = head.next;
            cur.next = pre;
            pre = cur;
            idx++;
        }
        rightNode = head;
        if (left == 1) dummyHead = rightNode;
        ListNode rightNext = rightNode.next;
        rightNode.next = pre;
        if (leftPre != null) leftPre.next = rightNode; //可能left 就是第一个节点，没有前驱节点
        leftNode.next = rightNext;
        return dummyHead;
    }




}
